package io.kiki.sba.registry.common.model.store;


public class Watcher extends BaseInfo {

    private volatile long pushedVersion;

    /**
     * change watcher word cache
     */
    public static Watcher internWatcher(Watcher watcher) {
        watcher.setRegisterId(watcher.getRegisterId());
        watcher.setDataInfoId(watcher.getDataInfoId());
        watcher.setInstanceId(watcher.getInstanceId());
        watcher.setGroup(watcher.getGroup());
        watcher.setDataId(watcher.getDataId());
        watcher.setClientId(watcher.getClientId());
        watcher.setZone(watcher.getZone());
        watcher.setProcessId(watcher.getProcessId());
        watcher.setAppName(watcher.getAppName());
        watcher.setClientUrl(Url.internURL(watcher.getClientUrl()));
        watcher.setServerUrl(Url.internURL(watcher.getServerUrl()));
        watcher.setAttributes(watcher.getAttributes());

        return watcher;
    }

    @Override
    public DataType getDataType() {
        return DataType.watcher;
    }

    public synchronized boolean hasPushed() {
        return pushedVersion > 0;
    }

    public synchronized long getPushedVersion() {
        return pushedVersion;
    }

    public synchronized boolean updatePushedVersion(long v) {
        if (pushedVersion < v) {
            this.pushedVersion = v;
            return true;
        }
        return false;
    }

    public String shortDesc() {
        final StringBuilder sb = new StringBuilder(256);
        sb.append("dataInfoId=").append(getDataInfoId()).append(", ");
        sb.append("registerId=").append(getRegisterId()).append(", ");
        sb.append("pushed=").append(pushedVersion).append(", ");
        sb.append("sourceAddress=").append(getClientUrl().buildAddressString());
        return sb.toString();
    }
}
